<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_package_Description
{
    private static $instances;

    /* @var $xml f_xml_XmlElement */
    private $xml;
    private $filePath;
    private $type;
    private $category;
    private $name;
    private $version;
    private $status;
    private $author;
    private $description;
    private $i18nDescription;
    private $extFiles;

	/**
	 * @return f_package_Description
	 */
	private function __construct($filePath)
	{
        if(is_file($filePath))
        {
            $this->filePath = $filePath;
            $this->xml = f_xml_Xpath::getInstance($filePath);
            $this->type = $this->xml->getElementValueFromXpath('type');
            $this->category = $this->xml->getElementValueFromXpath('category');
            $this->name = $this->xml->getElementValueFromXpath('name');
            $this->version = $this->xml->getElementValueFromXpath('version');
            $this->status = $this->xml->getElementValueFromXpath('status');
            $this->author = $this->xml->getElementValueFromXpath('author');
            $this->description = $this->xml->getElementValueFromXpath('description');
            $this->i18nDescription = $this->xml->getElementValueFromXpath('i18n_desc');
            $this->extFiles = $this->xml->getElementsFromXpath('ext_files/file');
        }
        else
        {
            return null;
        }
	}

	/**
	 * Get singleton instance
	 *
	 * @return f_package_Description
	 */
	public static function getInstance($filePath)
	{
        $instId = md5($filePath);

		if(!isset(self::$instances[$instId]))
		{
			self::$instances[$instId] = new self($filePath);
		}
		return self::$instances[$instId];
	}

    public function  __toString()
    {
        return $this->type.'_'.$this->category.'_'.$this->name;
    }

    public function getLabel()
    {
        return $this->type.'_'.$this->category.'_'.$this->name;
    }

    public function getDir()
    {
        return dirname(realpath($this->filePath));
    }

    public function getComponents()
    {
        if($this->getCategory() == 'application')
        {
            return $this->xml->getElementsFromXpath('components/component');
        }
    }

    public function getType()
    {
        return $this->type;
    }

    public function getCategory()
    {
        return $this->category;
    }
    
    public function getName()
    {
        return $this->name;
    }

    public function getVersion()
    {
        return $this->version;
    }

    public function getStatusLetter()
    {
        switch ($this->getStatus())
        {
            case 'alpha':
                return 'a';
            break;

            case 'beta':
                return 'b';
            break;

            case 'stable':
                return '';
            break;

            default:
                return $this->getStatus();
            break;
        }
    }

    public function getStatus()
    {
        return strtolower($this->status);
    }

    public function getAuthor()
    {
        return trim(strip_tags($this->author));
    }

    public function getAuthorMail()
    {
        $mails = array();
        if(preg_match('~\b[^@\s<>]+@[^@\s<>]+~', $this->author))
        {
            preg_match_all('~\b[^@\s<>]+@[^@\s<>]+~', $this->author, $mails);
            return $mails[0];
        }
        return $mails;
    }

    public function getDescription()
    {
        if($this->i18nDescription != '')
        {
            return $this->i18nDescription;
        }
        return $this->description;
    }

    public function hasModuleRootNode()
    {
        return !is_null(f_file_Resolver::getModuleFilePath($this->name, 'config'.DS.'treeParser.xml'));
    }

    public function hasModuleFolderList()
    {
        return !is_null(f_file_Resolver::getModuleFilePath($this->name, 'config'.DS.'folderList.xml'));
    }

    public function getModuleRootNodeUrl()
    {
        if($this->category == 'module')
        {           
            if($this->hasModuleRootNode())
            {
                return m_backoffice_services_RootNodeService::getInstance()->getModuleRootNode($this->name)->getEditUrl();
            }
        }
        return '';
    }

    public function getExtFiles()
    {
        $result = array();
        if(count($this->extFiles) > 0)
        {
            foreach($this->extFiles as $file)
            {
                /* @var $file f_xml_XmlElement */
                $path = str_replace('/', DS, $file->getContent());
                $result[] = BASE_DIR.DS.$path;
            }
        }
        return $result;
    }
}